package project;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

class Bill {
    private Date date;
    private double amount;
    private String category;
    private String remark;
    private boolean isIncome;

    public Bill(Date date, double amount, String category, String remark, boolean isIncome) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.remark = remark;
        this.isIncome = isIncome;
    }

    public Date getDate() {
        return date;
    }

    public double getAmount() {
        return amount;
    }

    public String getCategory() {
        return category;
    }

    public String getRemark() {
        return remark;
    }

    public boolean isIncome() {
        return isIncome;
    }
}

class BudgetManager {
    private double budget;
    private double spent;

    public BudgetManager(double budget) {
        this.budget = budget;
    }

    public void addSpent(double amount) {
        this.spent += amount;
    }

    public double getRemainingBudget() {
        return budget - spent;
    }
}

class PersonalFinanceManager {
    private static List<Bill> bills = new ArrayList<>();
    private static BudgetManager budgetManager = new BudgetManager(1000); // 设置初始预算为1000


    private static void recordIncome(Scanner scanner) {
        System.out.print("请输入日期 (yyyy-MM-dd): ");
        String dateStr = scanner.nextLine();
        System.out.print("请输入金额: ");
        double amount = scanner.nextDouble();
        scanner.nextLine(); // 清除换行符
        System.out.print("请输入类别（如工资、奖金）: ");
        String category = scanner.nextLine();
        System.out.print("请输入备注: ");
        String remark = scanner.nextLine();

        if (amount <= 0) {
            System.out.println("收入金额必须为正数");
            return;
        }

        try {
            Date date = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            bills.add(new Bill(date, amount, category, remark, true));
            budgetManager.addSpent(0); // 只记录收入，不影响预算
            System.out.println("收入记录成功");
        } catch (Exception e) {
            System.out.println("日期格式错误");
        }
    }

    private static void recordExpense(Scanner scanner) {
        System.out.print("请输入日期 (yyyy-MM-dd): ");
        String dateStr = scanner.nextLine();
        System.out.print("请输入金额: ");
        double amount = scanner.nextDouble();
        scanner.nextLine(); // 清除换行符
        System.out.print("请输入类别: ");
        String category = scanner.nextLine();
        System.out.print("请输入备注: ");
        String remark = scanner.nextLine();

        if (amount <= 0) {
            System.out.println("支出金额必须为正数");
            return;
        }

        try {
            Date date = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            bills.add(new Bill(date, amount, category, remark, false));
            budgetManager.addSpent(amount);
            System.out.println("支出记录成功");
        } catch (Exception e) {
            System.out.println("日期格式错误");
        }
    }

    private static void displayBills() {
        System.out.println("收入记录:");
        for (Bill bill : bills) {
            if (bill.isIncome()) {
                System.out.println("日期(YYYY-MM-DD)" + bill.getDate() + "\n金额：" + bill.getAmount() + "\n类别：（如工资、奖金）" + bill.getCategory() + "\n备注：" + bill.getRemark());
            }
        }

        System.out.println("支出记录:");
        for (Bill bill : bills) {
            if (!bill.isIncome()) {
                System.out.println("日期(YYYY-MM-DD)" + bill.getDate() + "\n金额" + bill.getAmount() + "\n类别：" + bill.getCategory() + "\n备注：" + bill.getRemark());
            }
        }
    }

    private static void queryBills(Scanner scanner) {
        //类别查询
        System.out.print("请输入查询类别: ");
        String category = scanner.nextLine();

        System.out.println("查询结果:");
        for (Bill bill : bills) {
            if (bill.getCategory().equalsIgnoreCase(category)) {
                System.out.println(bill.getDate() + " - " + bill.getAmount() + " - " + (bill.isIncome() ? "收入" : "支出") + " - " + bill.getRemark());
            }
        }
    }

    private static void manageBudget(Scanner scanner) {
        System.out.print("请输入新的预算限额: ");
        double newBudget = scanner.nextDouble();
        budgetManager = new BudgetManager(newBudget);
        System.out.println("新的预算限额设置成功");
    }

    private static void monthlyStatistics() {
        double totalIncome = 0;
        double totalExpense = 0;

        for (Bill bill : bills) {
            if (bill.isIncome()) {
                totalIncome += bill.getAmount();
            } else {
                totalExpense += bill.getAmount();
            }
        }

        System.out.println("月度统计:");
        System.out.println("总收入: " + totalIncome);
        System.out.println("总支出: " + totalExpense);
        System.out.println("剩余预算: " + budgetManager.getRemainingBudget());
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("个人账单管理系统");
            System.out.println("1. 记录收入");
            System.out.println("2. 记录支出");
            System.out.println("3. 账单展示");
            System.out.println("4. 账单查询");
            System.out.println("5. 设置月度预算");
            System.out.println("6. 查看月度统计报告");
            System.out.println("7. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符

            switch (choice) {
                case 1:
                    recordIncome(scanner);
                    break;
                case 2:
                    recordExpense(scanner);
                    break;
                case 3:
                    displayBills();
                    break;
                case 4:
                    queryBills(scanner);
                    break;
                case 5:
                    manageBudget(scanner);
                    break;
                case 6:
                    monthlyStatistics();
                    break;
                case 7:
                    System.out.println("退出程序");
                    return;
                default:
                    System.out.println("无效选项，请重新选择");
            }
        }
    }
}

public class ZDTest1 {

}


